package com.cenwangkeji.resident.model;

import android.content.Context;

import com.cenwangkeji.resident.model.bean.FamilyMemberBean;
import com.cenwangkeji.resident.model.bean.User;
import com.google.gson.Gson;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;


/**
 * @松 on 2019/9/9 12:56
 */
public class OkHttpUtils {
    private static OkHttpClient okHttpClient;

    private static OkHttpClient getInstance() {
        if (okHttpClient == null) {
            synchronized (OkHttpUtils.class) {
                if (okHttpClient == null) {
                   HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                   // 设置日志打印级别
                    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                    okHttpClient = new OkHttpClient.Builder()
                            .connectTimeout(10, TimeUnit.SECONDS)//10秒连接超时
                            .writeTimeout(10, TimeUnit.SECONDS)//10m秒写入超时
                            .readTimeout(10, TimeUnit.SECONDS)//10秒读取超时
                            .addNetworkInterceptor(loggingInterceptor)
                            .build();
                }
            }
        }
        return okHttpClient;
    }

    //同步post
    public static String post(String json, String url) throws IOException {
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = getInstance().newCall(request).execute();
        return response.body().string();
    }

    //同步 get
    public static String get( String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = getInstance().newCall(request).execute();
        return response.body().string();
    }


    //文件
    public static String uploadImage( String url ,File file) throws IOException {
        RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file);
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("files", file.getName(), fileBody)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = getInstance().newCall(request).execute();
        return response.body().string();
    }


    //post 泛型
    public static <T> Observable post(final String url, final Map<Object,Object> map, final Class<T> tClass){
        return  Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(ObservableEmitter<T> emitter) throws Exception {
                map.put("mark", 3);
                map.put("token", User.getToken());
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = post(jsonObject.toString(),url);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, tClass));
            }
        }).compose(RxUtils.<T>io_main());
    }
}
